package com.wuxin.controller.api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxin.entity.UserInfo;
import com.wuxin.entity.vo.*;
import com.wuxin.result.Result;
import com.wuxin.service.UserInfoService;
import com.wuxin.util.JwtUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author wuxin
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/api/core/userInfo")
public class UserInfoController {

    //  http://localhost/api/core/userInfo/register 提交 post

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @ApiOperation("会员注册")
    @PostMapping("/register")
    //vue前端以表单形式向后端传递数据时，要加@RequestBody，不然拿不到数据
    public Result register(@RequestBody RegisterVO registerVO){

        String mobile =  registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();

        //从redis中获取验证码
        System.out.println(mobile);
        String codeMsg = stringRedisTemplate.opsForValue().get("duanxin:" + mobile);
        System.out.println(codeMsg);
        System.out.println(mobile);
        //判断是否相等
        if (codeMsg == null) {
            return Result.error().message("验证码已过期或不存在");
        }
        if (!codeMsg.equals(code) ) {
            return Result.error().message("验证码错误");
        }
        return userInfoService.register(registerVO);
        //return Result.ok().message("注册成功");

    }

    // http://localhost/api/core/userInfo/login
    @ApiOperation("会员登录")
    @PostMapping("/login")
    private Result login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        //通过请求头获取 token 信息
        String token = request.getHeader("token");
        //获取ip
        String ip = request.getRemoteAddr();
        UserInfoVO userInfoVO = userInfoService.login(loginVO,ip);

        return Result.ok().data("userInfo",userInfoVO);
    }


    // /api/core/userInfo/auth/getIndexUserInfo
    @ApiOperation("会员登录")
    @GetMapping("/auth/getIndexUserInfo")
    private Result getIndexUserInfo(HttpServletRequest request) {
        //通过请求头获取token信息
        String token = request.getHeader("token");
        //获取用户id
        Long userId = JwtUtils.getUserId(token);

        UserIndexVO uiv = userInfoService.getUserIndexVoByUserId(userId);

        return Result.ok().data("userIndexVO",uiv);
    }


    // /api/core/userInfo/checkToken
    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    private Result checkToken(HttpServletRequest request) {
        //通过请求头获取token信息
        String token = request.getHeader("token");
        //获取用户id
        boolean result = JwtUtils.checkToken(token);

        if (result) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

/*    // http://localhost/admin/core/userInfo/list/1/10
    @ApiOperation("获取会员分页列表")
    @GetMapping("/list/{page}/{limit}")
    public Result listPage(
            @ApiParam(value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(value = "，每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询对象",required = true)
            @PathVariable UserInfoQuery userInfoQuery

    ){
        //mp中的分类页
        Page<UserInfo> pages = new Page<>(page,limit);

        IPage<UserInfo> infoIPage= userInfoService.listPage(pages,userInfoQuery);

        return Result.ok().data("pageModel",infoIPage);
    }*/

}

